/*
 * Copyright 2010    Sylvere RICHARD
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.infotrafficlux.server.dao;

import static com.google.infotrafficlux.server.service.ParkingUpdaterImpl.CENTRE;
import static com.google.infotrafficlux.server.service.ParkingUpdaterImpl.GARE;
import static com.google.infotrafficlux.server.service.ParkingUpdaterImpl.KIRCHBERG;
import static com.google.infotrafficlux.server.service.ParkingUpdaterImpl.PARK_AND_RIDE;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.google.infotrafficlux.server.bean.Parking;

/**
 * @author sly
 * 
 */
@Repository
public class ParkingDaoImpl implements ParkingDao {

	private EntityManager entityManager;

	public EntityManager getEntityManager() {
		return entityManager;
	}

	@PersistenceContext
	@Autowired
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Override
	public Long insertOrUpdate(Parking p) {
		if(p == null) {
			return new Long(-1);
		}
		Parking park = findByDesc(p.getDesc());
		
		if(park != null) {
			p.setId(park.getId());
		}
		getEntityManager().persist(p);
		return p.getId();

	}

	@Override
	public List<Parking> getAllParkings() {

		@SuppressWarnings("unchecked")
		List<Parking> places = (List<Parking>) getEntityManager().createQuery(
				"SELECT p FROM Parking p").getResultList();

		return places;
	}

	@Override
	public Parking findByDesc(String desc) {
		EntityManager em = getEntityManager();

		try {
			Query query = em
					.createQuery("SELECT b FROM Parking b WHERE desc = :desc");
			query.setParameter("desc", desc);

			return (Parking) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		} finally {
			safeClose(em);
		}
	}

	@Override
	public List<Parking> findByQuarter(String quarter) {
		EntityManager em = getEntityManager();

		try {
			Query query = em
					.createQuery("SELECT b FROM Parking b WHERE quarter = :quarter");
			query.setParameter("quarter", quarter);

			return query.getResultList();
		} catch (NoResultException nre) {
			return null;
		} finally {
			safeClose(em);
		}
	}

	
	void safeClose(EntityManager em) {
		try {
			em.close();
		} catch (Exception e) { // $codepro.audit.disable emptyCatchClause

		}

	}

	/* (non-Javadoc)
	 * @see com.google.infotrafficlux.server.dao.ParkingDao#getAllParkAndRide()
	 */
	@Override
	public List<Parking> getAllParkAndRide() {
		
		return findByQuarter(PARK_AND_RIDE);
	}

	/* (non-Javadoc)
	 * @see com.google.infotrafficlux.server.dao.ParkingDao#getAllCenter()
	 */
	@Override
	public List<Parking> getAllCenter() {
		
		return findByQuarter(CENTRE);
	}

	/* (non-Javadoc)
	 * @see com.google.infotrafficlux.server.dao.ParkingDao#getAllKirchberg()
	 */
	@Override
	public List<Parking> getAllKirchberg() {
		
		return findByQuarter(KIRCHBERG);
	}

	/* (non-Javadoc)
	 * @see com.google.infotrafficlux.server.dao.ParkingDao#getAllGare()
	 */
	@Override
	public List<Parking> getAllGare() {
		return findByQuarter(GARE);
	}
}
